home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news!enno
- From: enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner)
- Newsgroups: comp.lang.c++
- Subject: Re: simple design question
- Date: 02 Feb 1996 18:41:41 GMT
- Organization: Fachbereich Informatik, TH Darmstadt
- Distribution: world
- Message-ID: <ENNO.96Feb2194144@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- References: <DM4u8I.G1H@emr1.emr.ca>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- In-reply-to: jagrant@emr1.emr.ca's message of Fri, 2 Feb 1996 05:07:30 GMT
-
- In article <DM4u8I.G1H@emr1.emr.ca> jagrant@emr1.emr.ca (John Grant) writes:
-
- This is a design question, not a syntax question, so forgive the pseudo-
- code.
-
- I'm trying to design/write a class for complex file I/O (I'll state it
- in simple terms here).
-
- I want to use it like this:
- object.Open("xxx")
- for(...){
- object.Read(stuff);
- }
- object.Close();
-
- But I want to also have a single function that will Open/Read/Close in a
- single operation:
- object.GetData("xxx",stuff);
-
- The simple question is: should I implement GetData() as part of this
- class, i.e.:
- int myclass::GetData(...){
- { Open()
- Read()
- Close()
- }
-
- or should GetData() really be in a separate class which uses the first class:
- int myclass2::GetData(...)
- { myclass *object=new myclass;
- object->Open(...)
- object->Read(...)
- object->Close(...);
- delete object;
- }
-
- If so, is that the best way to do it, or is there a slicker C++ way to do
- that using inheritance etc (which I am just trying to figure out).
-
- The reason I ask, is the potential for conflict in the use of the
- private variables. For example, internally there might be:
- private: FILE *f;
- <...more internals...>
-
- If I used it incorrectly, i.e.:
- object.Open(...)
- object.GetData(...) //does Open/Read/Close
- object.Read(...)
- then object.Read() would break because GetData() calls Close(), invalidating
- 'f' and other internals.
-
- There is no problem with this approach as long the effects of the member-
- functions are documented. For example 'GetData' removes the connection to
- the stream and 'Read' operates on an object that is connected to a stream.
- Thus the sequence of method-invocations 'Open,GetData,Read' is not valid
- according to the semantics of the member-functions. In a robust program
- some sort of error-handling should cover such situations.
- Anyway I tend to your proposed solution to make 'GetData' an indendent
- function. Following this idea you can develop the system in layers which
- has been proven as quite useful technique.
-
- Enno
-